package fhp.hlhj.giantfold.activity.login

import android.app.AlertDialog
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.MainActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.interfaces.IPhoneLogin
import fhp.hlhj.giantfold.interfaces.IWeChatLogin
import fhp.hlhj.giantfold.javaBean.PhoneLoginBean
import fhp.hlhj.giantfold.module.OtherLoginBean
import fhp.hlhj.giantfold.presenter.PhoneLoginPresenter
import fhp.hlhj.giantfold.presenter.WechatLoginPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.SPUtils
import java.util.*

class Init2Aty : BaseActivity(), IWeChatLogin, IPhoneLogin {
    override fun showLoading() {
        Thread(Runnable {
            loadingDialog.show()
        })
    }

    private lateinit var wxLoginPresenter: WechatLoginPresenter
    private lateinit var phoneLoginPresenter: PhoneLoginPresenter
    override fun getContentId(): Int {
        return R.layout.activity_init2_aty
    }

    override fun initView() {
        wxLoginPresenter = WechatLoginPresenter(this)
        phoneLoginPresenter = PhoneLoginPresenter(this)

        val timer = Timer()
        val timeTask = object : TimerTask() {
            override fun run() {
                LogUtil.log("是否第一次$SPUtils.getIsFirst(this@Init2Aty")
                if (SPUtils.getIsFirst(this@Init2Aty)==0){
                    LogUtil.logi("开始自动登录${SPUtils.readLoginType(this@Init2Aty)}")
                    when (SPUtils.readLoginType(this@Init2Aty)) {
                        0 -> {
                            val params = HttpParams()
                            params.put("user_name", SPUtils.readUserName(this@Init2Aty))
                            params.put("password", SPUtils.readUserPwd(this@Init2Aty))
                            if (MyUtils.isNetworkAvailable(this@Init2Aty)) {

                                phoneLoginPresenter.login(params)
                            }
                            else {
                                AlertDialog.Builder(this@Init2Aty).setTitle("没有检查到网络连接")
                                        .setPositiveButton("确定") { dialog, which ->
                                            System.exit(0)
                                        }
                                        .show()
                            }
                        }
                        1 -> {
                            val httpParams = HttpParams()
                            httpParams.put("openid", SPUtils.readOpenId(this@Init2Aty))
                            httpParams.put("third_key", Contents.third_Key)
                            httpParams.put("login_type", 0)
                            if (MyUtils.isNetworkAvailable(this@Init2Aty)) {
                                wxLoginPresenter.loginWx(httpParams)

                            } else {
                                AlertDialog.Builder(this@Init2Aty).setTitle("没有检查到网络连接")
                                        .setPositiveButton("确定") { dialog, which ->
                                            System.exit(0)
                                        }
                                        .show()
                            }
                        }
                        2 -> {
                            startActivity(Intent(this@Init2Aty, MainActivity::class.java))
                            finish()
                        }
                        -1 -> {
                            startActivity(Intent(this@Init2Aty, MainActivity::class.java))
                            finish()
                        }
                    }
                }else{
                    SPUtils.saveIsFirst(this@Init2Aty,0)
                    startActivity(Intent(this@Init2Aty,InitAty::class.java))

                }
            }
        }
        timer.schedule(timeTask,2500)

    }
    /**
     * 手机登录
     */
    override fun login(phoneLoginBean: PhoneLoginBean) {
        if (phoneLoginBean.code==200){
            MyUtils.toast("登录成功")
            Contents.userInfo=phoneLoginBean.data
            Contents.userId=phoneLoginBean.data.id
            Contents.loginType=0
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }else{
            startActivity(Intent(this,MainActivity::class.java))
            MyUtils.toast(phoneLoginBean.msg)
        }
    }
    /**
     * 微信登录
     */
    override fun login(otherLoginBean: OtherLoginBean) {
        if (otherLoginBean.code==200){
            Contents.userInfo=otherLoginBean.data
            Contents.userId=otherLoginBean.data.id
            LogUtil.logi("登录成功${Contents.userId}")
            Contents.loginType=1
            SPUtils.saveLoginType(this,1)
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }else{
            startActivity(Intent(this,MainActivity::class.java))
            MyUtils.toast("登录失败")
        }
    }
}
